<?php
namespace app\shop\controller;
use app\base\model\CityM;
use app\rbac\model\AdminM;
use app\shop\model\ShopM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Shop extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new ShopM();
            $res = $model->add($data);
            if($res)
            {
                $this->success('新增成功',url('shop/Shop/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        $cityModel = new CityM();
        $cityData = $cityModel->getCityByLevel();
        $this->assign(array(
            '_page_title'=>'添加店铺',
            '_page_title_english'=>'Shop add',
            '_btn_title'=>'店铺列表',
            '_btn_link'=>'lst',
            '_page_ico'=>'iconfa-home',
            'cityData'=>$cityData,
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $adminModel = new AdminM();
        $res = $adminModel->isAuth($id,'shop_shop');
        if($res === false)
        {
            $this->error('没有权限!');
        }
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new ShopM();
            $res = $model->edit($data);
            if($res)
            {
                $this->success('修改成功',url('shop/Shop/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        //获取省列表
        $cityModel = new CityM();
        $cityData = $cityModel->getCityByLevel();
        //获取数据
        $data = Db::table('shop_shop')
            ->alias('s')
            ->join('city c1','c1.id = s.shi_id','LEFT')
            ->join('city c2','c2.id = s.xian_id','LEFT')
            ->where('s.id',$id)
            ->field('s.id,s.title,s.shop_intro,s.channel_id,s.coordinate,s.content,s.address,s.tel,s.contacts,s.sm_logo,s.sheng_id,s.shi_id,s.xian_id,s.erweima,c1.name as shi_name,c2.name as xian_name')
            ->find();

        $this->assign(array(
            '_page_title'=>'编辑店铺',
            '_page_title_english'=>'Shop edit',
            '_btn_title'=>'店铺列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-home',
            'cityData'=>$cityData,
            'data'=>$data,
        ));
        return $this->fetch();
    }

    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {
        $model = new ShopM();
        $data = $model->lst(15);
        // 获取分页显示
        $page = $data->render();
        $this->assign(array(
            '_page_title'=>'店铺列表',
            '_page_title_english'=>'Shop list',
            '_btn_title'=>'添加店铺',
            '_btn_link'=>url('add'),
            '_page_ico'=>'iconfa-home',
            'data'=>$data,
            'page'=>$page,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'没有权限']);
        }
        $id = $this->request->post('id');
        $model = new ShopM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }
}
